package controllers

import (
	"bufio"
	"fmt"
	"github.com/astaxie/beego"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

type UserController struct {
	beego.Controller
}

func (this *UserController) Get() {
	//logs.SetLogger("file")
	//logs.SetLogger(logs.AdapterFile,`{"filename":"project.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10}`)
	////an official log.Logger
	//l := logs.GetLogger()
	//l.Println("this is a message of http")
	////an official log.Logger with prefix ORM
	//logs.GetLogger("ORM").Println("this is a message of orm")
	//
	//logs.Debug("my book is bought in the year of ", 2016)
	//logs.Info("this %s cat is %v years old", "yellow", 3)
	//logs.Warn("json is a type of kv like", map[string]int{"key": 2016})
	//logs.Error(1024, "is a very", "good game")
	//logs.Critical("oh,crash")

	//this.Ctx.WriteString("hello world GetInfo--")
	//this.Ctx.WriteString("getinfo response success")

	dir := "D:\\gocode"
	files, dirs, _ := GetFilesAndDirs(dir)

	for _, dir := range dirs {
		fmt.Printf("获取的文件夹为[%s]\n", dir)
	}

	for _, table := range dirs {
		temp, _, _ := GetFilesAndDirs(table)
		for _, temp1 := range temp {
			files = append(files, temp1)
		}
	}

	for _, table1 := range files {
		/*b, err := ioutil.ReadFile(table1)
		if err != nil {
			fmt.Print(err)
		}
		str := string(b)
		fmt.Println(str)*/
		str,_ := ReadFiles(table1)
		fmt.Println(str)

		fmt.Printf("获取的文件为[%s]\n", table1)
	}

	fmt.Printf("=======================================\n")
	xfiles, _ := GetAllFiles("./simplemath")
	for _, file := range xfiles {
		fmt.Printf("获取的文件为[%s]\n", file)
	}

	this.Ctx.WriteString("getinfo response success")
}

//获取指定目录下的所有文件和目录
func GetFilesAndDirs(dirPth string) (files []string, dirs []string, err error) {
	dir, err := ioutil.ReadDir(dirPth)
	if err != nil {
		return nil, nil, err
	}

	PthSep := string(os.PathSeparator)
	//suffix = strings.ToUpper(suffix) //忽略后缀匹配的大小写

	for _, fi := range dir {
		if fi.IsDir() { // 目录, 递归遍历
			dirs = append(dirs, dirPth+PthSep+fi.Name())
			GetFilesAndDirs(dirPth + PthSep + fi.Name())
		} else {
			// 过滤指定格式
			ok := strings.HasSuffix(fi.Name(), ".go")
			if ok {
				files = append(files, dirPth+PthSep+fi.Name())
			}
		}
	}

	return files, dirs, nil
}

//获取指定目录下的所有文件,包含子目录下的文件
func GetAllFiles(dirPth string) (files []string, err error) {
	var dirs []string
	dir, err := ioutil.ReadDir(dirPth)
	if err != nil {
		return nil, err
	}

	PthSep := string(os.PathSeparator)
	//suffix = strings.ToUpper(suffix) //忽略后缀匹配的大小写

	for _, fi := range dir {
		if fi.IsDir() { // 目录, 递归遍历
			dirs = append(dirs, dirPth+PthSep+fi.Name())
			GetAllFiles(dirPth + PthSep + fi.Name())
		} else {
			// 过滤指定格式
			ok := strings.HasSuffix(fi.Name(), ".go")
			if ok {
				files = append(files, dirPth+PthSep+fi.Name())
			}
		}
	}

	// 读取子目录下文件
	for _, table := range dirs {
		temp, _ := GetAllFiles(table)
		for _, temp1 := range temp {
			files = append(files, temp1)
		}
	}

	return files, nil
}
//读取文件内容
func ReadFiles(pathFile string) (datas []string, err error) {
	file, err := os.OpenFile(pathFile, os.O_RDWR, 0666)
	if err != nil {
		//fmt.Println("Open file error!", err)
		return nil,err
	}
	defer file.Close()//函数return 后执行这代码
	stat, err := file.Stat()
	if err != nil {
		panic(err)  // 报错中断程序
	}
	var size = stat.Size() //文件大小
	fmt.Println("file size=", size)
	buf := bufio.NewReader(file)
	i := 0
	for {
		line, err := buf.ReadString('\n')
		line = strings.TrimSpace(line)
		//fmt.Println(line)
		datas = append(datas,line)
		i++
		if err != nil {
			if err == io.EOF {
				fmt.Println("File read ok!")
				break
			} else {
				//fmt.Println("Read file error!", err)
				return datas,err
			}
		}
	}
	return datas, nil
}


func(this *UserController) GetInfo(){
	id := this.Ctx.Input.Param(":id")
	this.Ctx.WriteString("getInfo data, id = "+id)
}
func(this *UserController) GetNum(){
	//beego.Router("/user/:id([0-9]+)", &controllers.UserController{},"get:GetNum")
	//id := this.Ctx.Input.Param(":id")
	//this.Ctx.WriteString("getNum data, id = "+id)

	//beego.Router("/user/:username([\\w]+)", &controllers.UserController{},"get:GetNum")
	//username := this.Ctx.Input.Param(":username")
	//this.Ctx.WriteString("getNum data, username = "+username)

	//beego.Router("/user/*.*", &controllers.UserController{},"get:GetNum")
	//ext := this.Ctx.Input.Param(":ext")
	//path := this.Ctx.Input.Param(":path")
	//this.Ctx.WriteString("getNum data, path = "+path+" ext = "+ext)

    //beego.Router("/user/*", &controllers.UserController{},"get:GetNum")
	splat := this.Ctx.Input.Param(":splat")
	this.Ctx.WriteString("getNum data, splat = "+splat)
}

func (this *UserController) PostData(){
	splat := this.Ctx.Input.Param(":splat")
	ddd := this.Ctx.Input.Param(":ddd")
	this.Ctx.WriteString("postNum data, splat = "+splat)
	this.Ctx.WriteString("postNum data, ddd = "+ddd)
}